// packagePay/pages/new-project-import/new-project-import.js
const app = getApp()
import {
    getProjectDetailInfo,
    getPayDetailList,
    addFromProject,
    addeleSignProject
} from '../../api/insurance'
import {
    payCopyWorkerProject
} from '../../../api/app'
import user from '../../../utils/user'

Page({
    /**
     * 页面的初始数据
     */
    data: {
        isDelete: false,
        projectId: '',
        insuranceId: '',
        projectInfo: {},
        pageNum: 1,
        searchValue: '',
        men: 0,
        women: 0,
        list: [],
        hasNextPage: false,
        count: 0,
        requestType: ''
    },
    async onLoad(options) {

        console.log(options, 'options')
        this.setData({
            projectId: options.ids,
            insuranceId: options.id,
            requestType: options.type
        })
    },

    async onReady() {
        await user.waitLogin()
        this.getProjectDetailInfo()
        this.getPayDetailList()
    },
    onShow() {

    },
    ShowDeterminePopup() {
        this.setData({
            isDelete: true
        })
    },
    cancel() {
        this.setData({
            isDelete: false
        })
    },
    async onAddPersonnel() {
        const arr = []
        arr.push(this.data.projectId)
        if (this.data.requestType === 'pay') {
            await payCopyWorkerProject({
                id: this.data.insuranceId,
                ids: arr
            })
            this.cancel()
            await app.toast('人员导入成功')
            const eventChannel = this.getOpenerEventChannel()
            eventChannel.emit('dataCallback', {
                data: 'payData'
            })
            await wx.navigateBack()
        } else if (this.data.requestType === 'eleSign') {
            await addeleSignProject({
                projectId: this.data.insuranceId,
                oldProjectId: this.data.projectId
            })
            this.cancel()
            await app.toast('人员导入成功')
            await wx.navigateBack()
        } else {
            await addFromProject({
                id: this.data.insuranceId,
                ids: arr
            })
            this.cancel()
            await app.toast('人员导入成功')
            await wx.navigateBack()
        }

    },
    // 获取项目信息
    async getProjectDetailInfo() {
        const result = await getProjectDetailInfo({
            id: this.data.projectId
        })
        console.log(result, 'result')
        this.setData({
            projectInfo: result
        })
    },
    // 获取项目人员列表
    async getPayDetailList() {
        const result = await getPayDetailList({
            projectId: this.data.projectId,
            searchText: this.data.searchValue,
            pageSize: 100,
            pageNum: this.data.pageNum,
        })
        result.list.map((item, index) => {
            item.idCardNoCut = item.idCardNo.substring(14)
            return item
        })
        console.log(result.list, 'list')
        this.setData({
            count: result.count,
            list: result.list,
            hasNextPage: result.hasNextPage,
            men: result.men,
            women: result.women
        })
        console.log(result, 'result')
    },
    cancel1() {
        this.setData({
            isDelete: false
        })
    },
    noGoprojectDetail() {
        console.log('点击啦')
        wx.navigateTo({
            url: '/packagePay/pages/new-resident-detail/new-resident-detail?id=' + this.data.projectId,
        })
    },
})